<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Manger\Manger;
use App\Models\Manger\Mangerrule;
use App\Models\Manger\MenulistNnew;
use App\Services\DingDing\DingDingSer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class MenuTestController extends Controller
{


    public function doexport(Request $request)
    {

        $file=storage_path('菜单.xlsx');
        $data = Excel::toArray(null, $file)[0];

        // print_r($data);exit;
/*
        一级菜单	二级菜单	三级菜单   0~2
        	按钮	4管理员1	5销售7	6采购6	7操盘2	8统审4	9出纳8	10票务3	11仓库5 3~11

            1管理员 2操盘 3票务 4统审 5仓库 6采购 7销售 8出纳

            	path	name	component	icon  12~15
        */
        foreach($data as $k=>$v){
            if($k==0){
                continue;
            }
            $rule_type=[
                $v[4]=='是'?1:'',
                $v[5]=='是'?7:'',
                $v[6]=='是'?6:'',
                $v[7]=='是'?2:'',
                $v[8]=='是'?4:'',
                $v[9]=='是'?8:'',
                $v[10]=='是'?3:'',
                $v[11]=='是'?5:'',
            ];
            $rule_type=array_filter($rule_type, function($value) {
                return $value>0;
            });
            $rule_typestr=implode(',',$rule_type);

            $max_rule_type=[
                $v[17]=='是'?1:'',
                $v[18]=='是'?7:'',
                $v[19]=='是'?6:'',
                $v[20]=='是'?2:'',
                $v[21]=='是'?4:'',
                $v[22]=='是'?8:'',
                $v[23]=='是'?3:'',
                $v[24]=='是'?5:'',
            ];


            $max_rule_type=array_filter($max_rule_type, function($value) {
                return $value>0;
            });
            $max_rule_typestr=implode(',',$max_rule_type);

            $menu1_exist=MenulistNnew::where(['title'=>$v[0],'menu_lv'=>1])->first();
            if(!$menu1_exist){

                $lv1data=[
                    'pid'=>0,
                    'name'=>$v[13]?$v[13]:'',
                        'path'=>$v[12]?$v[12]:'',
                        'component'=>$v[14]?$v[14]:'',
                    'title'=>$v[0]?$v[0]:'',
                    'icon'=>$v[15]?$v[15]:'',
                    'props'=>$v[16]?$v[16]:'',
                    'rule_typestr'=>$rule_typestr,
                    'max_rule_typestr'=>$max_rule_typestr,
                    'menu_lv'=>1,
                ];
                $lv1_id=MenulistNnew::create($lv1data)->id;
            }else{
                $lv1_id=$menu1_exist->id;
            }

            if($v[1]){

                // if($v[1]=='销售费用付款表'){
                //     Log::info("销售费用付款表");
                // }


                $menu2_exist=MenulistNnew::where(['title'=>$v[1],'menu_lv'=>2])->first();
                if(!$menu2_exist){
                    $lv2data=[
                        'pid'=>$lv1_id,
                        'name'=>$v[13]?$v[13]:'',
                        'path'=>$v[12]?$v[12]:'',
                        'component'=>$v[14]?$v[14]:'',
                        'title'=>$v[1]?$v[1]:'',
                        'icon'=>$v[15]?$v[15]:'',
                        'props'=>$v[16]?$v[16]:'',
                        'rule_typestr'=>$rule_typestr,
                        'max_rule_typestr'=>$max_rule_typestr,
                        'menu_lv'=>2,
                    ];

                    // if($v[1]=='销售费用付款表'){
                    //     Log::info("销售费用付款表1");
                    //     Log::info($lv2data);
                    // }
                    $lv2_id=MenulistNnew::create($lv2data)->id;
                }else{
                    $lv2_id=$menu2_exist->id;
                }
            }else{
                $lv2_id=0;
            }


            if($v[2]){
                $menu3_exist=MenulistNnew::where(['title'=>$v[2],'menu_lv'=>3])->first();
                if(!$menu3_exist){
                    $lv3data=[
                        'pid'=>$lv2_id,
                        'name'=>$v[13]?$v[13]:'',
                        'path'=>$v[12]?$v[12]:'',
                        'component'=>$v[14]?$v[14]:'',
                        'title'=>$v[2]?$v[2]:'',
                        'icon'=>$v[15]?$v[15]:'',
                        'props'=>$v[16]?$v[16]:'',
                        'rule_typestr'=>$rule_typestr,
                        'max_rule_typestr'=>$max_rule_typestr,
                        'menu_lv'=>3,
                    ];
                    $lv3_id=MenulistNnew::create($lv3data)->id;
                }else{
                    $lv3_id=$menu3_exist->id;
                }
            }else{
                $lv3_id=0;
            }



            if($v[3]){
                if($lv1_id>0){
                    $pid=$lv1_id;
                }

                if($lv2_id>0){
                    $pid=$lv2_id;
                }

                if($lv3_id>0){
                    $pid=$lv3_id;
                }


                $lv3data=[
                    'pid'=>$pid,
                    'name'=>$v[3],
                    'is_botton'=>1,
                    'rule_typestr'=>$rule_typestr,
                    'max_rule_typestr'=>$max_rule_typestr,
                ];
                $lv3_id=MenulistNnew::create($lv3data)->id;
            }


            /**
             * 一级
             *     一级分类
             *        财务、采购
             *     二级分类
             *        财务、采购
             *
             *
             *
             */



        }







    }

    public function dotestsetmenu(){


        $list=Manger::get()->toArray();
        // print_r($list);exit;
        foreach($list as $val){
            $manger_id=$val['id'];
            $utypes=explode(',',$val['utypes']);
            // print_r($utypes);
            foreach($utypes as $utype){
                $this->setMangerRole($manger_id,$utype);
            }

        }


    }



    public  function setMangerRole($manger_id,$utype){
        $select_arr = ['id', 'pid', 'name', 'path', 'props','hidden', 'component', 'always_show', 'title', 'icon',  'is_botton',
            //  'is_default as is_checked',
             DB::raw('(if(FIND_IN_SET('.$utype.',rule_typestr),1,0)) as is_checked')
            ];
            $menulist = MenulistNnew::where(['pid' => 0])->WhereRaw('FIND_IN_SET(?,max_rule_typestr)', [$utype])
                ->select($select_arr)->get();


            foreach ($menulist as &$val) {
                $buttonList = MenulistNnew::where(['pid' => $val->id, 'is_botton' => 1])->WhereRaw('FIND_IN_SET(?,max_rule_typestr)', [$utype])->select($select_arr)->get();
                $val->meta = ['title' => $val->title, 'icon' => $val->icon,'buttonList'=>$buttonList];
                unset($val->title);
                unset($val->icon);
                $submenu = MenulistNnew::where(['pid' => $val->id, 'is_botton' => 0])->WhereRaw('FIND_IN_SET(?,max_rule_typestr)', [$utype])->select($select_arr)->get();
                $val->props=$val->props?json_decode($val->props,true):[];
                foreach ($submenu as &$vval) {
                    $vval_buttonList = MenulistNnew::where(['pid' => $vval->id, 'is_botton' => 1])->WhereRaw('FIND_IN_SET(?,max_rule_typestr)', [$utype])->select($select_arr)->get();
                    $vval->meta = ['title' => $vval->title, 'icon' => $vval->icon,'buttonList'=>$vval_buttonList];
                    $vval->props=$vval->props?json_decode($vval->props,true):[];

                    unset($vval->title);
                    unset($vval->icon);


                    $subsubmenu = MenulistNnew::where(['pid' => $vval->id, 'is_botton' => 0])->WhereRaw('FIND_IN_SET(?,max_rule_typestr)', [$utype])->select($select_arr)->get();

                    foreach ($subsubmenu as &$vvval) {
                        $vvval_buttonList = MenulistNnew::where(['pid' => $vvval->id, 'is_botton' => 1])->WhereRaw('FIND_IN_SET(?,max_rule_typestr)', [$utype])->select($select_arr)->get();

                        $vvval->meta = ['title' => $vvval->title, 'icon' => $vvval->icon,'buttonList'=>$vvval_buttonList];
                        $vvval->props=$vvval->props?json_decode($vvval->props,true):[];

                        unset($vvval->title);
                        unset($vvval->icon);

                    }
                    $vval->children =$subsubmenu;
                }
                $val->children = $submenu;
            }


            Redis::set('manger_user_menulist_' . $manger_id.'_'.$utype, json_encode($menulist));


            Mangerrule::updateOrCreate(['mid' => $manger_id,'utype'=>$utype],['ruleinfo' => json_encode($menulist)]);
            return;


            // echo json_encode($menulist);exit;
            // print_r($menulist);exit;

            exit;

            // return $this->getmenulistnew(json_decode(json_encode($menulist),true));exit;


            Redis::set('manger_user_menulist_'.$manger_id.'_'.$utype, json_encode($menulist));

            $roleinfo=Mangerrule::where(['mid' => $manger_id,'utype'=>$utype])->first();
            if($roleinfo){

                Mangerrule::where(['mid' => $manger_id,'utype'=>$utype])->update(['ruleinfo' => json_encode($menulist)]);
            }else{
                Mangerrule::create(['mid' => $manger_id,'utype'=>$utype,'ruleinfo' => json_encode($menulist)]);
            }
    }




    public function getmenulistnew($menulist,$manger_id,$utype){

            foreach ($menulist as $key => $val) {
                if ($val['is_checked'] == 0) {
                    unset($menulist[$key]);
                } else {
                    if(isset($val['children'])){
                        foreach ($val['children'] as $kkey => $vval) {
                            if ($vval['is_checked'] == 0) {
                                unset($menulist[$key]['children'][$kkey]);
                            }else{
                                if(isset($vval['children'])){
                                    foreach ($vval['children'] as $kkkey => $vvval) {
                                        if ($vvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['children'][$kkkey]);
                                        }else{
                                            if(isset($vvval['mate']['buttonList'])){
                                                foreach ($vvval['mate']['buttonList'] as $bbbkey => $bbbvval) {
                                                    if ($bbbvval['is_checked'] == 0) {
                                                        unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList'][$bbbkey]);
                                                    }
                                                }
                                                $menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList']=array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList']);
                                            }
                                        }

                                    }
                                    $menulist[$key]['children'][$kkey]['children']=array_values($menulist[$key]['children'][$kkey]['children']);
                                }

                                if(isset($vval['mate']['buttonList'])){
                                    foreach ($vval['mate']['buttonList'] as $bbkey => $bbvval) {
                                        if ($bbvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['mate']['buttonList'][$bbkey]);
                                        }
                                    }
                                    $menulist[$key]['children'][$kkey]['mate']['buttonList']=array_values($menulist[$key]['children'][$kkey]['mate']['buttonList']);
                                }
                            }

                        }
                        $menulist[$key]['children']=array_values($menulist[$key]['children']);
                    }

                    if(isset($val['mate']['buttonList'])){
                        foreach ($val['mate']['buttonList'] as $bkey => $bvval) {
                            if ($bvval['mate']['is_checked'] == 0) {
                                unset($menulist[$key]['mate']['buttonList'][$bkey]);
                            }
                        }
                        $menulist[$key]['mate']['buttonList']=array_values($menulist[$key]['mate']['buttonList']);
                    }

                }
            }




        // $menulist=array_values($menulist);
        // echo json_encode(['code' => '000', 'msg' => '操作成功', 'data' => $menulist]);exit;
        // print_r($menulist);exit;

    }

}
